package com.download.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.download.backstage.entity.Dictionary;
import com.download.web.entity.BusDicDictionary;
import com.download.web.entity.BusDicItem;
import com.download.web.entity.SysDicDictionary;
import com.download.web.entity.model.Page;
import com.download.web.mapper.BusDicDictionaryMapper;
import com.download.web.mapper.BusDicItemMapper;
import com.download.web.service.IBusDicItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cmy
 * @since 2019-02-20
 */
@Service
public class BusDicItemServiceImpl extends ServiceImpl<BusDicItemMapper, BusDicItem> implements IBusDicItemService {

    @Resource
    private BusDicItemMapper itemMapper;
    @Resource
    private BusDicDictionaryMapper dicDictionaryMapper;

    @Override
    public Page<Dictionary> dictionaryPage(Integer pageIndex, Integer pageSize) {
        List<BusDicDictionary> dicDictionaries=dicDictionaryMapper.dictionaries(pageIndex*pageSize,pageSize);
        List<Dictionary> dictionaries=dicDictionaries.stream().map(d->{
            List<String> items = itemValues(d.getId());
            return new Dictionary(d.getId(),d.getValue(),items);
        }).collect(Collectors.toList());
        return new Page<>(dictionaries,dicDictionaryMapper.dictionariesCount(),pageIndex,pageSize);
    }

    public List<String> itemValues(Integer dictId) {
        return itemMapper.itemValueList(dictId);
    }

    @Override
    public List<BusDicItem> itemsByDictId(Integer dictId) {
        return this.list(new QueryWrapper<BusDicItem>().eq("dictionary_id",dictId));
    }
}
